package cn.tedu.ximart.server.controller;

import cn.tedu.ximart.server.constant.WebConst;
import cn.tedu.ximart.server.pojo.dto.AlbumAddNewDTO;
import cn.tedu.ximart.server.pojo.dto.AlbumUpdateDTO;
import cn.tedu.ximart.server.pojo.vo.AlbumListItemVO;
import cn.tedu.ximart.server.service.IAlbumService;
import cn.tedu.ximart.server.web.JsonPage;
import cn.tedu.ximart.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "相册管理模块")
@RestController
@RequestMapping("/albums")
public class AlbumController {

    @Autowired
    public IAlbumService albumService;

    @ApiOperation("增加相册")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read')")
    public JsonResult<Long> addNew(@Valid AlbumAddNewDTO albumAddNewDTO) {
        log.debug("接受到客户端提交的请求参数{}", albumAddNewDTO);
        Long id=albumService.addNew(albumAddNewDTO);
        return JsonResult.ok(id);
    }


    @ApiOperation("删除相册")
    @ApiOperationSupport(order = 201)
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read')")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("AlbumController.delete{}",id);
        albumService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("批量删除相册")
    @ApiOperationSupport(order = 202)
    @PostMapping("ids/delete")
    public JsonResult deleteByIds(@RequestBody Long... ids){
        log.info("AlbumController.deleteByIds{}",ids);
        albumService.deleteByIds(ids);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 301)
    @ApiOperation(value = "更新相册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "albumId", value = "相册id", paramType = "path", required = true, dataType = "long")
    })
    @PostMapping("/{albumId:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long albumId, @Valid AlbumUpdateDTO albumUpdateDTO) {
        albumService.updateById(albumId, albumUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult list() {
        log.debug("接收到查询相册列表的请求");
        List<AlbumListItemVO> albums = albumService.list();
        return JsonResult.ok(albums);
    }
}
